package com.flyduck.modules.system.web;

import com.flyduck.common.annotation.Log;
import com.flyduck.modules.system.domain.SysLog;
import com.flyduck.modules.system.service.SysLogService;
import com.flyduck.common.vo.AjaxResult;
import com.flyduck.common.vo.PageResult;
import com.flyduck.common.vo.QueryVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: flyduck-admin
 * @description:
 * @author: flyduck
 * @create: 2020-05-29 16:58
 **/
@Controller
@RequestMapping("/log")
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 首页
     * @return
     */
    @Log(value = "跳转到日志管理页面")
    @RequiresPermissions("log:list")
    @GetMapping()
    public String index(){
        return "system/log";
    }

    /**
     * 首页数据
     * @param queryVo
     * @return
     */
    @Log(value = "日志首页数据")
    @RequiresPermissions("log:list")
    @PostMapping("list")
    @ResponseBody
    public PageResult findList(QueryVo queryVo){
        return sysLogService.findPage(queryVo);
    }

    /**
     * 根据日志id删除日志
     * @param id
     * @return
     */
    @Log(value = "根据日志id删除日志")
    @RequiresPermissions("log:del")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysLogService.deleteById(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("删除失败");
        }
        return ajaxResult;
    }

    /**
     * 根据id查询日志
     * @return
     */
    @Log(value = "根据id查询日志")
    @GetMapping("/getLogById")
    @ResponseBody
    public SysLog getUserById(Long id){
        return sysLogService.getLogById(id);
    }
}
